//
//  HHConversationDemoViewController.m
//  HHChatKit
//
//  Created by Henry on 2021/5/10.
//

#import "HHConversationDemoViewController.h"
#import "HHMessagesDemoViewController.h"
#import "HHConversationModel.h"
#import "HHChatConfiguration.h"
#import <HHTool/HHTool.h>
#import "HHCreateChatDemoViewController.h"

@interface HHConversationDemoViewController () <HHConversationViewControllerDelegagte>
{
    BOOL _isDark;
}

@property (nonatomic, strong) NSMutableArray *users;

@property (nonatomic, strong) HHUserModel *loginUser;

@end

@implementation HHConversationDemoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = self;

    [self loadUsers];
    [self configChat];
    
    [self requestData];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"创建会话" style:UIBarButtonItemStylePlain target:self action:@selector(onClickRight)];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"暗黑模式" style:UIBarButtonItemStylePlain target:self action:@selector(onClickLeft)];

}

- (void)onClickLeft {
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    if (@available(iOS 13.0, *)) {
        _isDark = !_isDark;
        
        keyWindow.overrideUserInterfaceStyle = _isDark ? UIUserInterfaceStyleDark : UIUserInterfaceStyleLight;

    } else {
        // Fallback on earlier versions
    }
}

- (void)loadUsers {
    NSString *file = [[NSBundle mainBundle] pathForResource:@"User.plist" ofType:nil];
    NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:file];
    NSMutableArray *users = [NSMutableArray array];
    for (NSDictionary *dic in array) {
        HHUserModel *user = [HHUserModel modelWithUserId:dic[@"userId"] name:dic[@"name"]];
        [users addObject:user];
    }
    self.users = users;
}

- (void)configChat {
    [HHChatConfiguration shareInstance].defaultAvatarCorner = 5;
    [HHChatConfiguration shareInstance].languageType = HCLanguageChineseSimplified;
    
    self.loginUser = self.users[0];
    
    [HHChatConfiguration shareInstance].loginId = self.loginUser.userId;
    [HHChatConfiguration shareInstance].loginName = self.loginUser.name;
}

- (void)onClickRight {
    HHCreateChatDemoViewController *vc = [[HHCreateChatDemoViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)requestData {
    self.tableView.loading = YES;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self reloadData];
    });
    
}

- (void)reloadData {
    HHConversationModel *model1 = [[HHConversationModel alloc] init];
    model1.msg = @"自己";
    model1.unread = 20;
    model1.members = @[self.loginUser];
    model1.time = @"1632634978000";
    model1.identifier = @"1001";

    HHConversationModel *model2 = [[HHConversationModel alloc] init];
    model2.msg = @"2人";
    model2.unread = 0;
    model2.members = @[self.loginUser, self.users[1]];
    model2.time = @"1620704245000";
    model2.identifier = @"1002";

    HHConversationModel *model3 = [[HHConversationModel alloc] init];
    model3.msg = @"333";
    model3.unread = 0;
    model3.members = @[self.loginUser, self.users[1], self.users[2]];
    model3.time = @"1628404001000";
    model3.identifier = @"1003";

    HHConversationModel *model4 = [[HHConversationModel alloc] init];
    model4.msg = @"444";
    model4.unread = 0;
    model4.members = self.users;
    model4.time = @"1628231201000";
    model4.identifier = @"1004";
    
    HHConversationModel *model5 = [[HHConversationModel alloc] init];
    model5.name = @"555,2,3";
    model5.msg = @"555,2,3";
    model5.unread = 0;
    model5.members = self.users;
    model5.top = YES;
    model5.time = @"1628666195160";
    model5.identifier = @"1005";

    self.dataArray = @[model1, model2, model3, model4, model5].mutableCopy;
    [self sortDataList:self.dataArray];
    
    [self.tableView reloadData];
}

#pragma mark - HHConversationViewControllerDelegagte

- (void)conversationDidSelectNoNet {
    NSLog(@"无网络");
}

- (void)conversationDidSelectRow:(HHConversationModel *)conversation indexPath:(NSIndexPath *)indexPath {
    HHConversationModel *model = self.dataArray[indexPath.row];
    HHMessagesDemoViewController *vc = [[HHMessagesDemoViewController alloc] init];
    vc.conversationId = model.identifier;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)conversationConfigCell:(HHConversationCell *)cell indexPath:(NSIndexPath *)indexPath {
    // TODO: 配置相关cell
    
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self removeIndexPath:indexPath];
    }
}

@end
